home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4850 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  64 lines

  1. Path: bh.marintek.sintef.no!phi
  2. From: phi@bh.marintek.sintef.no (Per Henning Isaksen)
  3. Newsgroups: comp.lang.c
  4. Subject: typedefs for functions??
  5. Date: 7 Feb 1996 13:02:56 GMT
  6. Organization: MARINTEK, the SINTEF Group
  7. Message-ID: <4fa7u0$516@stork.runit.sintef.no>
  8. NNTP-Posting-Host: bh.marintek.sintef.no
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11.  
  12.  
  13. The following code works as intended, but I would like to
  14. change its 'looks'.
  15.  
  16.      1
  17.      2  typedef void (*ActionFunction)(char* g);
  18.      3
  19.      4  void a(char* b, ActionFunction g) {
  20.      5       ActionFunction p=g;
  21.      6       (*p)(b);
  22.      7       return;
  23.      8  }
  24.      9  void aa(char* g) { /* an ActionFunction */
  25.     10       printf("..%s..\n", g);
  26.     11       return;
  27.     12  }
  28.     13
  29.     14
  30.     15  void main() {
  31.     16       a("adf", aa);
  32.     17  }
  33.  
  34. Purpose: Inside a Motif GUI, I want to pass a function that
  35. can perform some action, so I have the typdef in line 2 to
  36. define ActionFunction. 
  37.  
  38. I would like to change line 9 to:
  39.    9  ActionFunction aa(char* g) {
  40. But this causes a warning on line 16 (but it executes correctly)
  41.    warning 604: Pointers are not assignment-compatible.
  42.    warning 563: Argument #2 is not the correct type.
  43.  
  44. Of course, I could use a cast, but I wouldn't like it.
  45.  
  46. Anyone who can teach me the trick? 
  47.  
  48. I want to state the function
  49. type (via typdefs) both in the parameter list and for the declaration
  50. of the function itself. (Why? I have lots of functions and I want want
  51. to mark those that are ActionFunction as such - because some change must
  52. be done to all functions of this type, but not to other function of 
  53. similar type (i.e. void (char*) in the example)). Obviously, I could
  54. use a comment, but any error that can be caught by the compiler is
  55. an error I can forget.
  56.  
  57.  
  58.  
  59. Per Henning
  60.  
  61.  
  62.  
  63.  
  64.